package com.administrator.facedete;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private ImageView imageView;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        textView = (TextView)findViewById(R.id.tofacetest);
        imageView =(ImageView)findViewById(R.id.myimage);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int rc = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
                if (rc == PackageManager.PERMISSION_GRANTED) {
                    Intent intent = new Intent(mContext, FaceDetectRGBActivity.class);
                    startActivity(intent);

//                    Intent intent = new Intent();
//                    intent.setClass(MainActivity.this, FaceDetectRGBActivity.class);
//                    startActivityForResult(intent,3);
                } else {
                    requestCameraPermission(1);
                }
            }
        });

        TestObServernotice.getInstance().addObserver(deleteObserver);
    }


    private Observer deleteObserver = new Observer() {
        @Override
        public void update(int id,Bitmap param) {
            imageView.setImageBitmap(param);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TestObServernotice.getInstance().removeObserver(deleteObserver);
        deleteObserver = null;
        Log.v("lgq", "......11..onDestroy....");
    }

    private void requestCameraPermission(final int RC_HANDLE_CAMERA_PERM) {
//        Log.w(TAG, "Camera permission is not granted. Requesting permission");

        final String[] permissions = new String[]{Manifest.permission.CAMERA};

        ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 3) {
            if (resultCode == RESULT_OK) {

                String param = data.getStringExtra("test");
//                Log.i("lgq", "startPhotoZoom==fragment===="+"......."+param);
                Bitmap bitmap= stringToBitmap(param);
                imageView.setImageBitmap(bitmap);
            }
        }
    }


    public Bitmap stringToBitmap(String string) {
        // 将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i("lgq","hhhh=---");

    }

}
